﻿
Type.registerNamespace('Kevin.Controls');

$Button = Kevin.Controls.Button = function(element) {

    $Button.initializeBase(this, [element]);

    this._waitingMessage = "服务器正在处理，请稍后...";
    this._confirmMessage = null;
    this._CallbackID = null;

    this._waitingWindow = null;
    this._confirmWindow = null;

    this.btnConfirmOk = null;
    this.btnConfirmCancel = null;
    this._onConfirmOkClick$delegate = {
        click: Function.createDelegate(this, this._onConfirmOkClick)
    }

    this._onConfirmCancelClick$delegate = {
        click: Function.createDelegate(this, this._onConfirmCancelClick)
    }

}

$Button.prototype = {

    initialize: function() {

        $Button.callBaseMethod(this, 'initialize');

        handler = Function.createDelegate(this, this._onClick);

        $addHandler($get(this.get_element().id), "click", handler);

        this._buildWaitingWindow();
        if (this._confirmMessage != null) {
            this._buildConfirm();
        }

    },
    dispose: function() {
        //$clearHandlers(this.get_element());
        
        $Button.callBaseMethod(this, 'dispose');
    },
    _buildWaitingWindow: function() {
        var divWait = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["content"],
            properties: {
                id: "divWait_" + this.get_element().id
            }
        }, document.body);

        divWait.innerHTML = this._waitingMessage;
        this._waitingWindow = $create(Sys.Extended.UI.ModalPopupBehavior,
                        { "DropShadow": false,
                            "PopupControlID": divWait.id,
                            "PopupDragHandleControlID": divWait.id,
                            "id": "ModalPopupBehavior" + this.get_element().id,
                            "BackgroundCssClass": "modalBackground",
                            "repositionMode": 2,
                            "OkControlID": this.get_element().id,
                            "isIE6": true
                        }, null, null, this.get_element());
    },
    _onClick: function(e) {
        if (this._confirmMessage != null) {
            this._confirmWindow.show();
            e.preventDefault();
        }
    },
    _doPost: function() {
        if (this._confirmWindow) {
            this._confirmWindow.hide();
        }
        this._waitingWindow.show();
        this.get_element().disabled = "true";
        //alert("before post");
        __doPostBack(this._CallbackID, null);
        //sleep(5000);
        //alert("after post");
        //e.preventDefault();
    },
    _buildConfirm: function() {

        var divID = this.get_element().id;
        var divConfirm = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["dialog"],
            properties: {
                id: "divConfirm" + divID
            }
        }, document.body);
        divConfirm.style.width = "200px";
        var divConfirmTitle = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["title"],
            properties: {
                id: "divConfirmTitle" + divID
            }
        }, divConfirm);
        var divConfirmTitle_info = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["info"],
            properties: {
                id: "divConfirmTitle_info" + divID

            }
        }, divConfirmTitle);
        divConfirmTitle_info.innerHTML = this.get_element().value + "确认?";
        var divConfirmTitle_close = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["ico"],
            properties: {
                id: "divConfirmTitle_close" + divID

            }
        }, divConfirmTitle);
        var divConfirm_body = $common.createElementFromTemplate({
            nodeName: "div",
            cssClasses: ["content"],
            properties: {
                id: "divConfirm_body" + divID

            }
        }, divConfirm);

        //divConfirm_body.style.width = "200px";
        divConfirm_body.style.height = "150px";
        var divConfirm_content = $common.createElementFromTemplate({
            nodeName: "div",
            properties: {
                id: "divConfirm_content" + divID

            }
        }, divConfirm_body);
        divConfirm_content.innerHTML = "<br><br>" + this._confirmMessage + "<br><br><br><hr>";



        var table_confirm = $common.createElementFromTemplate({ nodeName: "table" }, divConfirm_body);
        //table_confirm.style.width = "100%";
        var table_confirm_tr = $common.createElementFromTemplate({ nodeName: "tr" }, table_confirm);
        var table_confirm_td = $common.createElementFromTemplate({ nodeName: "td", properties: { align: "right"} }, table_confirm_tr);
        table_confirm_td.style.align = "right";
        this.btnConfirmOk = $common.createElementFromTemplate({
            nodeName: "input",
            events: this._onConfirmOkClick$delegate,
            properties: {
                id: "divConfirm_btnOK" + divID,
                type: "button",
                value: "确定"
            }
        }, divConfirm_content);
        this.btnConfirmCancel = $common.createElementFromTemplate({
            nodeName: "input",
            events: this._onConfirmCancelClick$delegate,
            properties: {
                id: "divConfirm_btnCancel" + divID,
                type: "button",
                value: "取消"
            }
        }, divConfirm_content);

        this._confirmWindow = $create(Sys.Extended.UI.ModalPopupBehavior,
                        { "DropShadow": false,
                            "PopupControlID": divConfirm.id,
                            "PopupDragHandleControlID": divConfirmTitle.id,
                            "id": "ModalPopupBehaviorConfirm" + this.get_element().id,
                            "BackgroundCssClass": "modalBackground",
                            "repositionMode": 2,
                            "OkControlID": this.btnConfirmOk.id,
                            "CancelControlID": divConfirmTitle_close.id,
                            "isIE6": true
                        }, null, null, this.get_element());

    },
    _onConfirmOkClick: function() {
        if (this._confirmWindow) {
            this._confirmWindow.hide();
            this._doPost();
        }
    },
    _onConfirmCancelClick: function() {
        if (this._confirmWindow) {
            this._confirmWindow.hide();
        }
    },
    get_waitingMessage: function() {
        return this._waitingMessage;
    },
    set_waitingMessage: function(value) {
        this._waitingMessage = value;
    },
    get_confirmMessage: function() {
        return this._confirmMessage;
    },
    set_confirmMessage: function(value) {
        this._confirmMessage = value;
    },
    get_CallbackID: function() {
        return this._CallbackID;
    },
    set_CallbackID: function(value) {
        this._CallbackID = value;
    }
}
// Optional descriptor for JSON serialization.
$Button.descriptor = {
    properties: [{ name: 'waitingMessage', type: String },
             { name: 'CallbackID', type: String },
             { name: 'confirmMessage',type:String}]
}

// Register the class as a type that inherits from Sys.UI.Control.
$Button.registerClass('Kevin.Controls.Button', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
//</Snippet11>